<?php
class v1_subscribe{
    function index(){
		$data['basic']=$this->yum->config['Manage']['domain'].'?c=subscribe&a=index';
		$params=array();
		if($this->yum->get('order',true)){
			$data['order']=$this->yum->get('order',true);$data['rule']=$this->yum->get('rule',true);
			if(in_array($data['order'],array('frae','leave','locked'))&&in_array($data['rule'],array('desc','asc'))){
				$params['order'][$data['order']]=$data['rule'];
			}
		}else{
			$params['order']['date']='desc';
		}
		if($this->yum->get('leave',true)){
			$data['leave']=explode(',',$this->yum->get('leave',true));
			if(count($data['leave'])==2){
				$params['where']=array('A.leave>='=>strtotime($data['leave'][0]),'A.leave<='=>strtotime($data['leave'][1]));
				$data['basic'].='&leave='.$data['leave'][0].','.$data['leave'][1];
			}
		}
		if($this->yum->get('locked',true)){
			$temp=$this->yum->get('locked',true);
			if(is_array($temp)){
				foreach($temp as $key=>$value){
					$data['locked'][$value]=intval($value);
					$data['basic'].='&locked[]='.$value;
				}
				$params['where']['A.locked IN']=$data['locked'];
			}
		}
		if($this->yum->get('search',true)){
			$data['search']=urldecode($this->yum->get('search',true));$data['tab']=$this->yum->get('tab',true);
			if($data['search']!=''){
				if($data['tab']=='member'){
					$params['where']['B.user LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='train'){
					$params['where']['A.train LIKE']='%'.strtoupper($data['search']).'%';
				}else if($data['tab']=='name'){
					$params['where']['A.name LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='number'){
					$params['where']['A.number LIKE']='%'.$data['search'].'%';
				}
				$data['basic'].='&tab='.$data['tab'];
				$data['basic'].='&search='.urlencode($data['search']);
			}
		}
		$session=$this->yum->library('session');
		$data['role']=intval($session->GVAR('role'));
		if($data['role']!=1){
			$params['where']['A.member=']=intval($session->GVAR('member'));
		}
		$subscribe=$this->yum->model('subscribe');
		$data['total']=$subscribe->GSubscribeFull(array('select'=>'COUNT(*) AS count','where'=>$params['where']));
		$data['total']=empty($data['total'])?0:$data['total'][0]['count'];
		$params['limit']=array(intval($this->yum->get('start')),15);
		$data['subscribe']=$subscribe->GSubscribeFull($params);
		$name=array();
		foreach($data['subscribe'] as $key=>$value){
			$name[]=$value['frae'];$name[]=$value['to'];$name[]=$value['seat'];
			$name[]=$value['kind'];$name[]=$value['certificate'];
		}
		if(!empty($name)){
			$sort=$this->yum->model('sort');
			$data['sortname']=$sort->GSort(array('where'=>array('id IN'=>$name,'locked='=>0)));
		}
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$data['basic'].'&order='.$data['order'].'&rule='.$data['rule'],'total_rows'=>$data['total'],'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
		$this->yum->view('index.php');
    }
    function leave(){
		$output=array('result'=>0,'msg'=>'');
		$data['id']=intval($this->yum->post('id'));$data['leave']=intval($this->yum->post('leave'));
		$subscribe=$this->yum->model('subscribe');
		$T=reset($subscribe->GSubscribe(array('where'=>array('id='=>$data['id']))));
		if(empty($T)){
			$output['msg']='错误的ID.';
		}else{
			$data['leave']=(((int)(($T['leave']+28800)/86400))*86400)-28800+$data['leave']*3600;
			$pams=array('set'=>array('`leave`='=>$data['leave']),'where'=>array('id='=>$data['id']));
			$session=$this->yum->library('session');
			$data['role']=intval($session->GVAR('role'));
			if($data['role']!=1){
				$pams['where']['member=']=intval($session->GVAR('member'));
			}
			$subscribe->USubscribe($pams);
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
    }
    function append(){
		if($this->yum->post('save')){
			$session=$this->yum->library('session');
			$output=array('result'=>0,'msg'=>'');
			$data['user']=$this->yum->post('user',true);$data['password']=$this->yum->post('password',true);$data['train']=strtoupper($this->yum->post('train',true));
			$data['frae']=explode('|',$this->yum->post('frae',true));$data['to']=$this->yum->post('to',true);$data['leave']=strtotime($this->yum->post('leave'));
			$data['seat']=$this->yum->post('seat');$data['kind']=$this->yum->post('kind');$data['name']=$this->yum->post('name');
			$data['certificate']=$this->yum->post('certificate');$data['number']=$this->yum->post('number');$data['mobile']=$this->yum->post('mobile');
			$check=true;
			foreach($data as $key=>$value){
				if(empty($value)){
					$check=false;break;
				}
			}
			if($check){
				$data['leave']=$data['leave']+$data['frae'][2]*3600;
				$data['frae']=$data['frae'][0];
				$data['member']=$session->GVAR('member');
				$data['date']=$this->yum->runtime;
				$subscribe=$this->yum->model('subscribe');
				$affected=$subscribe->ISubscribe($data);
				if($affected==1){
					$output['result']=2;
					$output['msg']='添加成功.';
				}else{
					$output['msg']='错误的表单数据.';
				}
			}else{
				$output['msg']='请完整填写表单数据.';
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
		}else{
			$cache=$this->yum->cache(array('tag'=>'subscribe/append.php','time'=>999999999));
			if($cache===false){
				$sort=$this->yum->model('sort');
				$data['char']=$sort->GSort(array('where'=>array('label='=>'station','parent='=>0,'locked='=>0)));
				$data['station']=$sort->GSort(array('order'=>array('parent'=>'ASC'),'where'=>array('label='=>'station','parent!='=>0,'locked='=>0)));
				$data['seat']=$sort->GSort(array('where'=>array('label='=>'seat','locked='=>0)));
				$data['kind']=$sort->GSort(array('where'=>array('label='=>'kind','locked='=>0)));
				$data['certificate']=$sort->GSort(array('where'=>array('label='=>'certificate','locked='=>0)));
				$this->yum->set($data);
				$this->yum->view('append.php',array('tag'=>'subscribe/append.php','strip'=>true,'caching'=>true));
			}else{
				echo $cache;
			}
		}
    }
    function edit(){
		$subscribe=$this->yum->model('subscribe');
		if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');$session=$this->yum->library('session');
			$params['where']['id=']=intval($this->yum->post('id'));
			if(intval($session->GVAR('role'))!=1){
				$params['where']['member=']=intval($session->GVAR('member'));
			}
			$check=$subscribe->GSubscribe($params);
			$check=reset($check);
			if($check['locked']==1||$check['locked']==10){
				$data['user=']=$this->yum->post('user',true);$data['password=']=$this->yum->post('password',true);$data['train=']=strtoupper($this->yum->post('train',true));
				$data['frae=']=explode('|',$this->yum->post('frae',true));$data['`to`=']=intval($this->yum->post('to'));$data['`leave`=']=strtotime($this->yum->post('leave'));
				$data['seat=']=intval($this->yum->post('seat'));$data['kind=']=intval($this->yum->post('kind'));$data['name=']=$this->yum->post('name',true);
				$data['certificate=']=intval($this->yum->post('certificate'));$data['number=']=$this->yum->post('number',true);$data['mobile=']=$this->yum->post('mobile',true);
				$data['locked=']=1;
				$check=true;
				foreach($data as $key=>$value){
					if(empty($value)){
						$check=false;break;
					}
				}
				if($check){
					$data['`leave`=']=$data['`leave`=']+$data['frae='][2]*3600;
					$data['frae=']=$data['frae='][0];
					$affected=$subscribe->USubscribe(array('set'=>$data,'where'=>$params['where']));
					if($affected==1){
						$output['result']=2;
						$output['msg']='预订修改成功.';
					}else{
						$output['msg']='请误提交未经修改的数据.';
					}
				}else{
					$output['msg']='请完整填写表单数据.';
				}
			}else{
				$output['msg']='预定目前不可编辑.';
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
		}else{
			$session=$this->yum->library('session');
			$params['where']['id=']=intval($this->yum->get('subscribe'));
			if(intval($session->GVAR('role'))!=1){
				$params['where']['member=']=intval($session->GVAR('member'));
			}
			$data['subscribe']=$subscribe->GSubscribe($params);
			$data['subscribe']=reset($data['subscribe']);
			$sort=$this->yum->model('sort');
			$data['char']=$sort->GSort(array('where'=>array('label='=>'station','parent='=>0,'locked='=>0)));
			$data['station']=$sort->GSort(array('order'=>array('parent'=>'ASC'),'where'=>array('label='=>'station','parent!='=>0,'locked='=>0)));
			$data['seat']=$sort->GSort(array('where'=>array('label='=>'seat','locked='=>0)));
			$data['kind']=$sort->GSort(array('where'=>array('label='=>'kind','locked='=>0)));
			$data['certificate']=$sort->GSort(array('where'=>array('label='=>'certificate','locked='=>0)));
			$this->yum->set($data);
			$this->yum->view('edit.php');
		}
    }
    function locked(){
		$output=array('result'=>0,'msg'=>'');
		$subscribe=$this->yum->model('subscribe');
		$id=$this->yum->get('subscribe');
		$locked=intval($this->yum->get('locked'));
		$session=$this->yum->library('session');
		$role=intval($session->GVAR('role'));
		if(is_array($id)){
			$id=array_map('intval',$id);
			$affected=$subscribe->USubscribe(array('where'=>array('id IN'=>$id),'set'=>array('locked='=>$locked)));
			if($affected==0){
				$output['msg']='重复的数据提交.';
			}else{
				$output['result']=2;
			}
		}else{
			$affected=$subscribe->USubscribe(array('where'=>array('id='=>intval($id)),'set'=>array('locked='=>$locked)));
			if($affected==1){
				$output['result']=5;
				$output['td']=array('offset'=>$role==1?11:10,'data'=>array($locked==2?'已审核':'无效'));
				$output['exec']=array('attr'=>array('mixed'=>'sub:{locked:'.($locked==2?10:2).'}'),'text'=>($locked==2?'无效':'通过'));
			}else{
				$output['msg']='状态已变更.';
			}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
    }
    function wipe(){
    	$params=array('where'=>array('locked IN'=>array(1,4,10)));
    	$session=$this->yum->library('session');
		$data['role']=intval($session->GVAR('role'));
		if($data['role']!=1){
			$params['where']['member=']=intval($session->GVAR('member'));
		}
 		$output=array('result'=>0,'msg'=>'');
		$subscribe=$this->yum->model('subscribe');
		$id=$this->yum->get('subscribe');
		$session=$this->yum->library('session');
		if(is_array($id)){
			$id=array_map('intval',$id);$params['where']['id IN']=$id;
			$affected=$subscribe->DSubscribe($params);
			if($affected==0){
				$output['msg']='当前订单不可删除.';
			}else{
				$output['result']=2;
			}
		}else{
			$params['where']['id=']=intval($id);
			$affected=$subscribe->DSubscribe($params);
			if($affected==1){$output['result']=1;}else{$output['msg']='当前订单不可删除.';}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
    }
    function ticket(){
    	$subscribe=$this->yum->model('subscribe');
    	$session=$this->yum->library('session');
		$params['where']['id=']=intval($this->yum->get('subscribe'));
		if(intval($session->GVAR('role'))!=1){
			$params['where']['member=']=intval($session->GVAR('member'));
		}
		$data['subscribe']=$subscribe->GSubscribe($params);
		$data['subscribe']=reset($data['subscribe']);
		$sort=$this->yum->model('sort');
		$sort=$sort->GSort(array('where'=>array('id IN'=>array($data['subscribe']['frae'],$data['subscribe']['to']))));
		$data['frae']=$sort[$data['subscribe']['frae']];$data['to']=$sort[$data['subscribe']['to']];
		$frae=explode('|',$data['frae']['value']);$to=explode('|',$data['to']['value']);
		$mmstr=$this->yum->model('mmstr');
		$data['V']=$mmstr->GMmstr(array('where'=>array('date>='=>date('Y-m-d'),'`frae`='=>$frae[0],'`to`='=>$to[0])));
		$this->yum->set($data);
		$this->yum->view('ticket.php');
    }
}
?>